const express = require("express");
const route = express.Router();
const adminModel = require("../db/model/adminModel")
const md5 = require('md5');
// const e = require("express");
const {
  jwtEncode
} = require("../utils/jwt")
route.post("/login", async (req, res) => {
  const {
    adminName,
    password
  } = req.body

  //校验是否登入成功
  try {
    const findResult = await adminModel.findOne({
      adminName,
      password: md5(password + '3388')
    })
    if (!findResult) {
      res.send({
        code: 10001,
        msg: '登入查询异常：账号或者密码错误 ',
        data: null
      })
    }
    const token = jwtEncode(findResult.adminName, "123");
    res.send({
      code: 10000,
      msg: '登入成功',
      data: {
        token
      }
    })
    //登录成功以后把数据库当前的用户状态改为1
    await adminModel.updateOne({
      adminName
    }, {
      $set: {
        status: 1
      }
    })

  } catch (e) {
    res.send({
      code: 10002,
      msg: '登入查询异常：' + e.message
    })
  }

})

module.exports = route;